package org.msb.accentis.gui.transactions;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.msb.accentis.data.pojo.Account;
import org.msb.accentis.data.pojo.Transaction;
import org.msb.accentis.data.pojo.TransactionDetail;

public abstract class AbstractTransactionAdapter {

	private Transaction currentTransaction;
	
	private Account currentAccount;
	
	private String reference;
	
	private Date date;
	
	protected AbstractTransactionAdapter(Transaction transaction, Account account) {
		currentTransaction = transaction;
		currentAccount = account;
		
		reference = transaction.getReference();
		date = transaction.getDate();
	}
	
	protected Account getCurrentAccount() {
		return currentAccount;
	}
	
	public Transaction getActualTransaction() {
		return currentTransaction;
	}
	
	public String getReference() {
		return reference;
	}
	
	public void setReference(String value) {
		reference = value;
	}
	
	public Date getDate() {
		return date;
	}
	
	public void setDate(Date value) {
		date = value;
	}
	
	public abstract BigDecimal getAmount();
	
	public void saveTransaction() {
		getActualTransaction().setReference(getReference());
		getActualTransaction().setDate(getDate());
	}
	
	public static AbstractTransactionAdapter adaptTransaction(Transaction transaction, Account account) {
		if (null == transaction)
			return null;
		
		List<TransactionDetail> details = transaction.getTransactionDetails();
		
		/*
		 * For transactions with 0 or 1 TransactionDetail, use SimpleTransactionAdapter.
		 */
		if (1 >= details.size())
			return new SimpleTransactionAdapter(transaction, account);
		
		/*
		 * For transactions with 2 TransactionDetails and a net value of 0.00, use the TransactionTransactionAdapter.
		 */
		if (2 == details.size() && 0 == transaction.getValue().compareTo(BigDecimal.ZERO))
			return new TransferTransactionAdapter(transaction, account);
		/*
		 * In all other cases, use the ComplexTransactionAdapter.
		 */
		return new ComplexTransactionAdapter(transaction, account);
	}
}
